--藏宝图
--#include "data\config\item\ItemTreasureMap.lua" once

local npcId = 101
NpcDialog[npcId] = {}
local t = NpcDialog[npcId]

t.main = function(sysarg)
	
	local var = Actor.getStaticVar(sysarg)
	if not var.days then
		var.days = {}
	end
	local times = var.days.treasureTimes or 0
	
	local lastTimes = TreasureMap.buyLimit-times
	if lastTimes < 0 then
		lastTimes = 0 
	end

	local strCond		= string.format(Lang.ScriptTips.x00222, TreasureMap.buyLevel, lastTimes)
	local strDesc 		= ""
	local okBtn 		= ""			--{按钮;样式;按钮名;函数名;描述}
	local strBtnList 	= ""
	
	for k, v in ipairs(TreasureMap) do
		if v.consume.type == 3 then
			strDesc = strDesc .. string.format(Lang.ScriptTips.x00217, v.consume.count) .. "\n"
			okBtn 		= "{btn;0;".. Lang.ScriptTips.x00223 ..";%s;}"			--{按钮;样式;按钮名;函数名;描述}
			strBtnList 	= strBtnList .. string.format(okBtn, string.format("BuyTreasureMap,%d", k))
		elseif v.consume.type == 10 then
			strDesc = strDesc .. string.format(Lang.ScriptTips.x00218, v.consume.count) .. "\n"
			okBtn 		= "{btn;0;".. Lang.ScriptTips.x00224 ..";%s;}"			--{按钮;样式;按钮名;函数名;描述}
			strBtnList 	= strBtnList .. string.format(okBtn, string.format("BuyTreasureMap,%d", k))
		end
	end
	local pack = DataPack.allocPacket(sysarg, LogicCmd.npcDialogSys.sysID, LogicCmd.npcDialogSys.sub.sNpcDlgData)

	DataPack.writeUint64(pack, Actor.getNpc(sysarg))
	DataPack.writeByte(pack, NpcDialogType.GeneralNpcDlg)
	DataPack.writeString(pack, strCond)
	DataPack.writeString(pack, strDesc)
	DataPack.writeString(pack, strBtnList)
	DataPack.writeChar(pack, -1)
	DataPack.writeByte(pack, 0);
	DataPack.flush(pack)
end

--购买藏宝图
t.BuyTreasureMap = function(sysarg, index)

	--local sVar = System.getStaticVar()
	--if not sVar.mapFlag then
	--	Actor.sendTipmsg(sysarg, Lang.ScriptTips.x00219, ttFlyTip)
	--	return
	--end

	if not Actor.checkActorLevel(sysarg, TreasureMap.buyLevel, 0) then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.x00226, ttFlyTip)
		return
	end

	local var = Actor.getStaticVar(sysarg)
	if not var.days then
		var.days = {}
	end
	local times = var.days.treasureTimes or 0
	if times >= TreasureMap.buyLimit then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.x00225, ttFlyTip)
		return
	end


	index = tonumber(index)
	local mapInfo = TreasureMap[index]
	if not mapInfo then
		return
	end

	local consumes = {mapInfo.consume}
	if not CheckConsumeCond(sysarg, consumes) then
		return 
	end

	local awards = {{type = 0, id = mapInfo.item_id, count = 1}}
	if not Awards.CheckBagGridCount(sysarg, awards) then
		return
	end

	var.days.treasureTimes = times + 1


	Actor.triggerAchieveEvent(sysarg, 44, 5, 1)

	DoConsumeCond(sysarg, consumes, 1, GameLog.clTreasureMapFee, Lang.ScriptTips.x00220)
	Awards.GiveNoCheck(sysarg, awards, GameLog.clTreasureMapAward, Lang.ScriptTips.x00221)
	if var.days.treasureTimes < TreasureMap.buyLimit then
		--Actor.closeNPCDialog(sysarg)
		t.main(sysarg)
	end
end


